package city.spring.modules.material.service.impl;

import city.spring.modules.material.entity.MaterialreceiptEntity;
import city.spring.modules.material.entity.MaterialreceiptapprovalEntity;
import city.spring.modules.material.repository.MaterialreceiptapprovalRepository;
import city.spring.modules.material.service.MaterialreceiptService;
import city.spring.modules.material.service.MaterialreceiptapprovalService;
import city.spring.modules.system.entity.UserEntity;
import city.spring.modules.system.service.UserService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.NonNull;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.cache.annotation.CacheConfig;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.security.Principal;
import java.util.Date;
import java.util.List;

/**
 * Service：领用审核
 *
 * @author 谢金婷
 * @date 2020-06-26 22:39:08
 */
@CacheConfig(cacheNames = {MaterialreceiptapprovalServiceImpl.CACHE_NAME})
@Transactional(rollbackFor = Throwable.class)
@Service
public class MaterialreceiptapprovalServiceImpl extends ServiceImpl<MaterialreceiptapprovalRepository, MaterialreceiptapprovalEntity> implements MaterialreceiptapprovalService {

    private static final Logger logger = LoggerFactory.getLogger(MaterialwarehousingServiceImpl.class);

    private final MaterialreceiptService materialreceiptService;
    private final UserService userService;
    private final MaterialreceiptapprovalRepository materialreceiptapprovalRepository;

    public MaterialreceiptapprovalServiceImpl(MaterialreceiptService materialreceiptService, UserService userService, MaterialreceiptapprovalRepository materialreceiptapprovalRepository) {
        this.materialreceiptService = materialreceiptService;
        this.userService = userService;
        this.materialreceiptapprovalRepository = materialreceiptapprovalRepository;
    }

    @Override
    public void saveReceiptapproval(MaterialreceiptapprovalEntity entity, @NonNull Principal user) {

        //获取当前登录用户信息
        UserEntity userEntity = userService.getUserByIdOrAccount(user.getName());
        entity.setReviewer(userEntity.getId());
        entity.setReviewdate(new Date());
        entity.setType("部门领导");


        if (!save(entity)) {
            throw new RuntimeException("保存信息失败");
        }
        //获取物资领用申请信息
        MaterialreceiptEntity materialreceiptEntity = materialreceiptService.getById(entity.getReceiptId());
        // 更新领用申请表部门领导审核信息
        materialreceiptEntity.setDReviewer(entity.getReviewer());
        materialreceiptEntity.setDReviewdate(entity.getReviewdate());
        materialreceiptEntity.setDApprovalStatus(entity.getApprovalStatus());
        materialreceiptEntity.setDSuggestion(entity.getSuggestion());
        materialreceiptService.lambdaUpdate()
                .set(MaterialreceiptEntity::getDReviewer, materialreceiptEntity.getDReviewer())
                .set(MaterialreceiptEntity::getDReviewdate, materialreceiptEntity.getDReviewdate())
                .set(MaterialreceiptEntity::getDApprovalStatus, materialreceiptEntity.getDApprovalStatus())
                .set(MaterialreceiptEntity::getDSuggestion, materialreceiptEntity.getDSuggestion())
                .eq(MaterialreceiptEntity::getId, materialreceiptEntity.getId())
                .update();


        //判断当前用户是否是该领用单领用人部门主管
//        if (userEntity.getId().equals(materialreceiptEntity.getDReviewer())) {
//            //设置领用申请单id
//            entity.setReceiptId(materialreceiptEntity.getId());
//            /**
//             * 修改领用申请单信息
//             */
//            //修改审核状态
//            materialreceiptEntity.setApprovalStatus(entity.getApprovalStatus());
//            //审核结果
//            materialreceiptEntity.setSuggestion(entity.getSuggestion());
//            //审核日期为当前系统日期
//            materialreceiptEntity.setReviewdate(new Date());
//
//            System.out.println("materialreceiptEntity.getUserReviewer()" + materialreceiptEntity.getUserReviewer());
//
//            // 调用更新方法
//            materialreceiptService.lambdaUpdate()
//                    .set(MaterialreceiptEntity::getApprovalStatus, materialreceiptEntity.getApprovalStatus())
//                    .set(MaterialreceiptEntity::getSuggestion, materialreceiptEntity.getSuggestion())
//                    .set(MaterialreceiptEntity::getReviewdate, materialreceiptEntity.getReviewdate())
//                    .eq(MaterialreceiptEntity::getId, materialreceiptEntity.getId())
//                    .update();
//        } else {
//            throw new RuntimeException("您不能审核该领用申请单，该领用单由领用人部门主管审核。");
//        }

    }

    @Override
    public void updateReceiptapproval(MaterialreceiptapprovalEntity entity) {
        // 使用 updateById(entity); 修改数据时，将会修改 entity 对象中所有非null数据，如果某个字段为null，将会忽略该字段的修改
        boolean update = updateById(entity);
        if (!update) {
            throw new RuntimeException("修改信息失败");
        }
    }

    @Override
    public void deleteReceiptapproval(String primaryKey) {
        removeById(primaryKey);
    }

    @Override
    public void deleteReceiptapproval(List<String> primaryKeys) {
        removeByIds(primaryKeys);
    }

    @Override
    public void saveReceiptapprovalOcheck(MaterialreceiptapprovalEntity entity, @NonNull Principal user) {
        //获取当前登录用户信息
        UserEntity userEntity = userService.getUserByIdOrAccount(user.getName());
        entity.setReviewer(userEntity.getId());
        entity.setReviewdate(new Date());
        entity.setType("院办");

        if (!save(entity)) {
            throw new RuntimeException("保存信息失败");
        }
        //获取物资领用申请信息
        MaterialreceiptEntity materialreceiptEntity = materialreceiptService.getById(entity.getReceiptId());
        // 更新领用申请表部门领导审核信息
        materialreceiptEntity.setOReviewer(entity.getReviewer());
        materialreceiptEntity.setOReviewdate(entity.getReviewdate());
        materialreceiptEntity.setOApprovalStatus(entity.getApprovalStatus());
        materialreceiptEntity.setOSuggestion(entity.getSuggestion());
        materialreceiptService.lambdaUpdate()
                .set(MaterialreceiptEntity::getOReviewer, materialreceiptEntity.getOReviewer())
                .set(MaterialreceiptEntity::getOReviewdate, materialreceiptEntity.getOReviewdate())
                .set(MaterialreceiptEntity::getOApprovalStatus, materialreceiptEntity.getOApprovalStatus())
                .set(MaterialreceiptEntity::getOSuggestion, materialreceiptEntity.getOSuggestion())
                .eq(MaterialreceiptEntity::getId, materialreceiptEntity.getId())
                .update();
    }

    @Override
    public List<MaterialreceiptapprovalEntity> getCheckByReceiptId(String receiptId) {
        return materialreceiptapprovalRepository.getCheckByReceiptId(receiptId);
    }
}